//
//  FKMyInfoVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2018/12/10.
//  Copyright © 2018 Macbook. All rights reserved.
//

#import "FKMyInfoVC.h"
#import "FKImageTool.h"
#import "FKModifyNicknameVC.h"
#import "FKDatePicker.h"
#import "FKSexPicker.h"
#import "FKCityPicker.h"

@interface FKMyInfoVC ()

@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UITextField *nicknameField;
@property (weak, nonatomic) IBOutlet UITextField *sexField;
@property (weak, nonatomic) IBOutlet UITextField *birthField;
@property (weak, nonatomic) IBOutlet UITextField *areaField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@property (nonatomic, strong) NSMutableDictionary *param;

@end

@implementation FKMyInfoVC

- (NSMutableDictionary *)param
{
    if (!_param) {
        _param = [NSMutableDictionary dictionary];
    }
    return _param;
}

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKMyInfoVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"bg_main"] forState:UIControlStateNormal];
    NSDictionary *userInfo = [FKCacheTool userInfo];
    [self.avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:kDefaultAvatar];
    self.nicknameField.text = userInfo[@"nickname"];
    if ([userInfo[@"sex"] isEqualToString:@"0"]) {
        self.sexField.text = @"女";
    } else if ([userInfo[@"sex"] isEqualToString:@"1"]) {
        self.sexField.text = @"男";
    }
    self.birthField.text = userInfo[@"birthday"];
    self.areaField.text = userInfo[@"region"];
    
    // 先给参数赋初始值【因为更新的时候这四个参数不能漏】
    self.param[@"nickname"] = self.nicknameField.text;
    self.param[@"sex"] = self.sexField.text;
    self.param[@"birthday"] = self.birthField.text;
    self.param[@"region"] = self.areaField.text;
}

- (IBAction)saveBtnClicked:(UIButton *)sender
{
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=set_userinfo" param:self.param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"更新成功"];
            fkPopVC;
        }
    } failure:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    fkWeakSelf(self);
    if (indexPath.section == 0) {
        FKImageTool *tool = [FKImageTool sharedInstance];
        [tool fk_showImageActionSheet];
        tool.getImageBlock = ^(UIImage *image) {
            self.avatarView.image = image;
            [weakself upload:image];
        };
    } else {
        if (indexPath.row == 0) {
            FKModifyNicknameVC *vc = [[FKModifyNicknameVC alloc] init];
            vc.nickname = self.nicknameField.text;
            vc.updateNickname = ^(NSString * _Nonnull nickname) {
                weakself.nicknameField.text = nickname;
                weakself.param[@"nickname"] = nickname;
            };
            fkPushNav(vc);
        } else if (indexPath.row == 1) {
            FKSexPicker *picker = [[FKSexPicker alloc] init];
            [picker show];
            picker.selectedBlock = ^(NSDictionary * _Nonnull data) {
                weakself.param[@"sex"] = data[@"sex"];
                weakself.sexField.text = data[@"title"];
            };
        } else if (indexPath.row == 2) {
            FKDatePicker *picker = [[FKDatePicker alloc] init];
            [picker show];
            picker.maximumDate = [NSDate date];
            picker.selectedDateBlock = ^(NSDate *date) {
                NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
                [dateFormat setDateFormat:@"yyyy-MM-dd"];
                weakself.birthField.text = [dateFormat stringFromDate:date];
                weakself.param[@"birthday"] = weakself.birthField.text;
            };
        } else if (indexPath.row == 3) {
            FKCityPicker *picker = [[FKCityPicker alloc] init];
            [picker show];
            picker.selectedCityBlock = ^(NSDictionary *dict) {
                self.areaField.text = fkFORMAT(@"%@ %@ %@", dict[@"province"], dict[@"city"], dict[@"area"]);
                self.param[@"region"] = dict[@"area"];
            };
        }
    }
}

//#pragma mark - 网络请求
//- (void)loadData
//{
//    NSDictionary *userData = [FKCacheTool userInfo];
//    NSDictionary *userInfo = userData[@"info"];
//    if (userInfo && userInfo.allKeys.count > 0) {
//        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:[UIImage imageNamed:@"information_head"]];
//        self.nicknameLabel.text = userInfo[@"nickname"];
//        self.mobileLabel.text = userInfo[@"mobile"];
//    }
//
//    [FKHttpTool postWithSuffix:@"m=App&c=Member&a=birthdayList" param:nil success:^(FKBaseResult *result) {
//        self.finish = true;
//        if (result.isSuccess) {
//            NSArray *list = result.data[@"list"];
//            self.birthdayList = list;
//            if (list.count > 0) {
//                self.birthLabel.text = @"已完善";
//            }
//        }
//    } failure:^(NSError *error) {
//        self.finish = true;
//    }];
//}

- (void)upload:(UIImage *)avatar
{
    [SVProgressHUD show];
    [FKHttpTool uploadFile:@"m=Home&c=Mine&a=set_photo" param:nil files:@{@"file" : avatar} progress:^(NSProgress *progress) {
        [SVProgressHUD showProgress:progress.fractionCompleted];
    } success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"头像上传成功!"];
        } else {
            [SVProgressHUD dismiss];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

@end
